﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class SachTimKiemTheoDanhMuc : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        string MaTKOL = "";
        string ds = ""; 
        PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    if (MyCookies["UserName"] != "" || MyCookies["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(MyCookies["UserName"].ToString());
                    }
                }
                else
                    if (Session["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(Session["UserName"].ToString());
                    }
            }
            ds=Request.QueryString["ds"];
            if (ds == "" || ds == null)
                Response.Redirect("SachTimKiemTheoDanhMuc.aspx?ds=GK");
            else
                Load_DauSach(ds);
            txt_theloaisach.Text = DauSach(ds);
            
        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select matkol from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        private string DauSach(string d)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select tendausach from dausach where kihieu='" + d + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        private void Load_DauSach(string d)
        {
            pds.AllowPaging = true;
            pds.PageSize = 5;
            pds.CurrentPageIndex = 0;
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%"+d+"%' ")));
            if (ds.Tables.Count > 0)
            {        
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["DonGiaLe"] = ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Replace(".0000", "");
                }
                pds.DataSource = ds.Tables[0].DefaultView;
                for (int i = 0; i < pds.PageCount; i++)
                {
                    LinkButton btn = new LinkButton();
                    btn.Text = (i + 1).ToString();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Width = 15;
                    Panel1.Controls.Add(btn);
                }
                //View1.Visible = true;
                //View2.Visible = false;
                if (!IsPostBack)
                {
                    dl_sach.DataSource = pds;
                    dl_sach.DataBind();
                }
            }
            else
            {
                //View1.Visible = false;
                //View2.Visible = true;
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            int trang = int.Parse(((LinkButton)sender).Text) - 1;
            pds.CurrentPageIndex = trang;
            dl_sach.DataSource = pds;
            dl_sach.DataBind();
        }

        protected void dl_sach_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "dat")
            {
                if (MaTKOL == "")
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Bạn chưa đăng nhập.');", true);
                else
                {
                    DataTable tbl;
                    if (Session["GioHang"] == null)
                    {
                        tbl = new DataTable();
                        tbl.Columns.Add("MaMH", typeof(string));
                        tbl.Columns.Add("TenMH", typeof(string));
                        tbl.Columns.Add("SoLuong", typeof(int));
                        tbl.Columns.Add("DonGia", typeof(double));
                        tbl.Columns.Add("KhuyenMai", typeof(float));
                        tbl.Columns.Add("ThanhTien", typeof(double), "SoLuong*DonGia");
                    }
                    else
                        tbl = (DataTable)Session["GioHang"];
                    bool trungma = true;
                    if (tbl.Rows.Count > 0)
                    {
                        for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            if (tbl.Rows[i]["MaMH"].ToString() == e.CommandArgument.ToString())
                            {
                                trungma = false;
                                tbl.Rows[i]["SoLuong"] = (int.Parse(tbl.Rows[i]["SoLuong"].ToString().Trim()) + 1).ToString();
                            }
                        }
                    }
                    if (trungma == true)
                    {
                        DataRow dr = tbl.NewRow();
                        dr["MaMH"] = e.CommandArgument.ToString();
                        HyperLink hb_tenmh = (HyperLink)e.Item.FindControl("hl_ten");
                        dr["TenMH"] = hb_tenmh.Text;
                        dr["SoLuong"] = 1;
                        Label lb_dongia = (Label)e.Item.FindControl("lb_dongia");
                        dr["DonGia"] = double.Parse(lb_dongia.Text.Replace(" VNĐ", ""));
                        dr["KhuyenMai"] = KM(e.CommandArgument.ToString());
                        tbl.Rows.Add(dr);
                    }
                    Session["GioHang"] = tbl;
                }
            }
        }
        private float KM(string mamh)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select khuyenmai from mathang where mamh='" + mamh + "'")));
            float km = float.Parse(ds.Tables[0].Rows[0][0].ToString());
            return km;
        }
    }
}